package com.yike.user.service.impl;

import com.yike.common.exception.BusinessException;
import com.yike.user.entity.Banner;
import com.yike.user.mapper.BannerMapper;
import com.yike.user.param.BannerParam;
import com.yike.user.service.BannerService;
import com.yike.user.param.BannerPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.BannerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 轮播图表 服务实现类
 *
 * @author Sakura
 * @since 2024-09-12
 */
@Slf4j
@Service
public class BannerServiceImpl extends BaseServiceImpl<BannerMapper, Banner> implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveBanner(BannerParam bannerParam) throws Exception {
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerParam, banner);
        return super.save(banner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateBanner(BannerParam bannerParam) throws Exception {
        Banner banner = bannerMapper.selectById(bannerParam.getId());
        if (banner == null) {
            throw new BusinessException("轮播图信息异常");
        }
        BeanUtils.copyProperties(bannerParam, banner);
        return super.updateById(banner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteBanner(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public BannerVo getBannerById(Long id) throws Exception {
        Banner banner = bannerMapper.selectById(id);
        if (banner == null) {
            throw new BusinessException("轮播图信息异常");
        }
        BannerVo bannerVo = new BannerVo();
        BeanUtils.copyProperties(banner, bannerVo);
        return bannerVo;
    }

    @Override
    public Paging<BannerVo> getBannerPageList(BannerPageParam bannerPageParam) throws Exception {
        Page<Banner> page = new PageInfo<>(bannerPageParam);
        IPage<BannerVo> iPage = bannerMapper.getBannerList(page, bannerPageParam);
        return new Paging<BannerVo>(iPage);
    }

}
